import nProgress from 'nprogress'
import { createRouter, createWebHashHistory } from 'vue-router'
import { useSysConfigStore } from '@/stores/sysConfig'
const modules = import.meta.glob('@/views/**/*.vue')

/**
 * 进度条配置
 */
nProgress.configure({
  easing: 'ease',
  showSpinner: false
})

export const router = createRouter({
  // history: createWebHistory(import.meta.env.BASE_URL),
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      name: 'ContainerView',
      component: () => import('@/views/layout/ContainerView.vue'),
      redirect: '/home',
      children: [
        {
          path: '/home',
          name: 'Home',
          component: () => import('@/views/Home.vue'),
        },
        {
          path: '/:catchAll(.*)',
          name: 'NotFound',
          meta: {
            title: '页面不存在',
            icon: 'location',
            keepalive: true
          },
          component: () => import('@/views/layout/NotFound.vue'),
        }
      ]
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/Login.vue'),
    }
  ],
})

//前置路由
router.beforeEach(async (to, from, next) => {

  //顶部进度条启动
  nProgress.start()

  const token = localStorage.getItem('pangu-authorization-token')

  if (to.path === '/login' && token) {
    next('/home')
    return
  }

  if (to.path !== '/login' && !token) {
    next('/login')
    return
  }

  next()

})

//后置路由
router.afterEach((to, from) => {
  nProgress.done()
})

export default router